<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
use app\common\library\Sms;


/**
 * 会员登录
 * Class Login
 * @package app\api\controller
 * 
 * @method mixed wx_openid_login()  		客户登录
 * @method mixed mer_login()				商户登录
 * @method mixed cus_login()				客服|业务登录
 */
class Login extends Api
{
	protected $noNeedLogin = ['accout', 'mobilelogin', 'wx_openid_login', 'mer_login', 'cus_login'];
	protected $noNeedRight = '*';

	/**
	 * 会员登录
	 *
	 * @ApiMethod (POST)
	 * @param string $account  账号
	 * @param string $password 密码
	 */
	public function accout()
	{
		$account = $this->request->post('account');
		$password = $this->request->post('password');
		if (!$account || !$password) {
			$this->error(__('参数错误'));
		}
		if ($this->check_user($account) === false) {
			$this->error(__('禁止登录'));
		}
		$ret = $this->auth->login($account, $password);
		if ($ret) {
			$finds = Db::name("user")->where(["mobile" => $account])->find();
			$data = ['userinfo' => $this->auth->getUserinfo()];
			$data = ['userinfo' => $this->auth->getUserinfo()];
			$data["userinfo"]["userType"] = $finds["is_typs"];
			$data["userinfo"]["idcards"] = $finds["idcards"];
			$data["userinfo"]["is_verify"] = $finds["is_verify"];
			$data["userinfo"]["is_bank"] = $finds["is_bank"];
			$data["userinfo"]["group_id"] = $finds["group_id"];
			$data["userinfo"]["admin_id"] = $finds["admin_id"];
			$this->success(__('登录成功'), $data);
		} else {
			$this->error($this->auth->getError());
		}
	}


	private function check_user($phone)
	{
		if (!$phone) {
			return false;
		}
		$find = Db::name("user")->where("mobile", $phone)->find();
		if ($find) {
			if ($find["is_typs"] == 2) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 手机验证码登录
	 *
	 * @ApiMethod (POST)
	 * @param string $mobile  手机号
	 * @param string $captcha 验证码
	 */
	public function mobilelogin()
	{
		$mobile = $this->request->post('mobile');
		$captcha = $this->request->post('captcha');
		$typs = $this->request->post("type", '');
		if (!in_array($typs, [1, 2])) {
			$this->error(__('禁止登录'));
		}
		if (!$mobile || !$captcha || !$typs) {
			$this->error(__('参数错误'));
		}
		if ($this->check_user($mobile) === false) {
			$this->error(__('禁止登录'));
		}
		if (!Validate::regex($mobile, "^1\d{10}$")) {
			$this->error(__('登录手机号格式错误'));
		}
		$sms = \app\common\func\Lib::getSms($mobile);
		if (!$sms || $sms->sms_code != $captcha) {
			$this->error(__('验证码错误'));
		}

		// 1=商家,2=客户,3=业务员,4=业务主管,5=客服
		// $user = \app\common\model\User::getByMobile($mobile);
		$where = ["mobile" => $mobile];
		switch ($typs) {
			case 1:
				$where["is_typs"] = $typs;
				break;
			case 2:
				$where["is_typs"] = ["in", '3,4,5'];
				break;
		}
		$user = \app\common\model\User::where($where)->find();
		if ($user) {
			if ($user->status != 'normal') {
				$this->error(__('账号禁止登录'));
			}
			//如果已经有账号则直接登录
			$ret = $this->auth->direct($user->id);
		} else {
			$this->error("用户不存在！");
		}
		if ($ret) {
			// Sms::flush($mobile, 'mobilelogin');
			$finds = Db::name("user")->where(["mobile" => $mobile])->find();
			$data = ['userinfo' => $this->auth->getUserinfo()];
			$data["userinfo"]["userType"] = $finds["is_typs"];
			$data["userinfo"]["idcards"] = $finds["idcards"];
			$data["userinfo"]["is_verify"] = $finds["is_verify"];
			$data["userinfo"]["is_bank"] = $finds["is_bank"];
			$data["userinfo"]["group_id"] = $finds["group_id"];
			$data["userinfo"]["admin_id"] = $finds["admin_id"];
			$this->success(__('登录成功'), $data);
		} else {
			$this->error($this->auth->getError());
		}
	}


	/**
	 * 客户登录
	 */
	public function wx_openid_login()
	{
		$openid = $this->request->post("openid");
		if (!$openid) {
			$this->error(__('参数错误'));
		}
		$finds = Db::name("user")->where(["wx_open_id" => $openid, 'is_typs' => 2])->find();
		if ($finds) {
			if ($finds["status"] != 'normal') {
				$this->error(__('账号禁止登录'));
			} else {
				$ret = $this->auth->direct($finds["id"]);
				if ($ret) {
					$data = ['userinfo' => $this->auth->getUserinfo()];
					$data["userinfo"]["userType"] = 2;
					$data["userinfo"]["idcards"] = $finds["idcards"];
					$data["userinfo"]["is_verify"] = $finds["is_verify"];
					$data["userinfo"]["is_bank"] = $finds["is_bank"];
					$data["userinfo"]["group_id"] = $finds["group_id"];
					$data["userinfo"]["admin_id"] = $finds["admin_id"];
					$this->logs($openid);
					$this->success(__('登录成功'), $data);
				} else {
					$this->error($this->auth->getError());
				}
			}
		} else {
			$this->error("用户不存在！！！");
		}
	}


	/**
	 * 访问
	 */
	public function logs($openid)
	{
		$times =  strtotime(date("Y-m-d", time()));
		$where = "times = {$times} and openid = '{$openid}'";
		$check = Db::name("logs_login")->where($where)->count();
		if ($check) {
			$update["nums"] = Db::raw("nums + 1");
			Db::name("logs_login")->where($where)->update($update);
		} else {
			$data = [
				"openid" => $openid,
				"nums" => 1,
				"times" => $times,
				"create_time" => time()
			];
			Db::name("logs_login")->insert($data);
		}
	}

	/**
	 * 商户登录
	 */
	public function mer_login()
	{
		$openid = $this->request->post("openid");
		if (!$openid) {
			$this->error(__('参数错误'));
		}
		$finds = Db::name("user")->where(["wx_open_id" => $openid, 'is_typs' => 1])->find();
		if ($finds) {
			if ($finds["status"] != 'normal') {
				$this->error(__('账号禁止登录'));
			} else {
				$ret = $this->auth->direct($finds["id"]);
				if ($ret) {
					$data = ['userinfo' => $this->auth->getUserinfo()];
					$data["userinfo"]["userType"] = 1;
					$data["userinfo"]["idcards"] = $finds["idcards"];
					$data["userinfo"]["is_verify"] = $finds["is_verify"];
					$data["userinfo"]["is_bank"] = $finds["is_bank"];
					$data["userinfo"]["group_id"] = $finds["group_id"];
					$data["userinfo"]["admin_id"] = $finds["admin_id"];
					$this->success(__('登录成功'), $data);
				} else {
					$this->error($this->auth->getError());
				}
			}
		} else {
			$this->error("用户不存在！！！");
		}
	}

	/**
	 * 客服|业务登录
	 */
	public function cus_login()
	{
		$openid = $this->request->post("openid");
		if (!$openid) {
			$this->error(__('参数错误'));
		}
		$finds = Db::name("user")
			->where('is_typs', 'in', [3, 4, 5])
			->where(["wx_open_id" => $openid])
			->find();
		if ($finds) {
			if ($finds["status"] != 'normal') {
				$this->error(__('账号禁止登录'));
			} else {
				$ret = $this->auth->direct($finds["id"]);
				if ($ret) {
					$this->auth->setAllowFields([
						"id",
						"username",
						"nickname",
						"mobile",
						"avatar",
						"group_id",
						"admin_id",
						"wx_open_id",
						"is_typs"
					]);
					$data = ['userinfo' => $this->auth->getUserinfo()];
					$data["userinfo"]["userType"] = 3;
					$data["userinfo"]["idcards"] = $finds["idcards"];
					$data["userinfo"]["is_verify"] = $finds["is_verify"];
					$data["userinfo"]["is_bank"] = $finds["is_bank"];
					$data["userinfo"]["group_id"] = $finds["group_id"];
					$data["userinfo"]["admin_id"] = $finds["admin_id"];
					$this->success(__('登录成功'), $data);
				} else {
					$this->error($this->auth->getError());
				}
			}
		} else {
			$this->error("用户不存在！！！");
		}
	}
}
